ALTER PROCEDURE [dbo].[CPP_Game_RichRank]
  @dwPageSize AS INT
AS
BEGIN

  IF @dwPageSize<=0 OR @dwPageSize>50 BEGIN
    SET @dwPageSize = 11
	END ELSE BEGIN
		SET @dwPageSize = @dwPageSize + 1
  END
  
	SELECT 0 AS Tab, t.* INTO #TABLEA FROM (
		SELECT
			ROW_NUMBER() OVER(PARTITION BY a.Channel ORDER BY Score DESC, a.LastLogonDate DESC) AS ROW_NUMBER,
			a.Channel,a.UserID,a.GameID,a.NickName,a.Gender,(b.Score + b.InsureScore) AS Score, e.HeadImgUrl AS Avatar
		FROM
			RYAccountsDB.dbo.AccountsInfo a
		LEFT JOIN RYTreasureDB.dbo.GameScoreInfo b ON b.UserID = a.UserID
		LEFT JOIN RYAccountsDB.dbo.IndividualDatum e ON e.UserID = a.UserID
		WHERE a.IsAndroid = 0 AND a.AccountsType = 0
	)t WHERE t.ROW_NUMBER < @dwPageSize


	SELECT 1 AS Tab, t.* INTO #TABLEB FROM (
		SELECT
			ROW_NUMBER() OVER(PARTITION BY d.Channel ORDER BY Score DESC) AS ROW_NUMBER,
			d.Channel, d.UserID, d.GameID, d.NickName, d.Gender, ISNULL(c.Score, 0) AS Score, e.HeadImgUrl AS Avatar
		FROM
			RYAccountsDB.dbo.AccountsInfo d
		LEFT JOIN (
			SELECT
				UserID, SUM (RMB) AS Score
			FROM RYWebSMSDB.dbo.ChargeOnline
			WHERE Status = 1 AND GameStatus = 1
			GROUP BY UserID
		) c ON  c.UserID = d.UserID
		LEFT JOIN RYAccountsDB.dbo.IndividualDatum e ON e.UserID = d.UserID
		WHERE d.IsAndroid = 0 AND d.AccountsType = 0
	)t WHERE t.ROW_NUMBER < @dwPageSize

	SELECT * FROM #TABLEA UNION ALL SELECT * FROM #TABLEB
	DROP TABLE #TABLEA
	DROP TABLE #TABLEB

  RETURN 0
END